package com.github.cyf.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.github.cyf.common.module.response.Rs;
import com.github.cyf.domain.po.Permission;
import com.github.cyf.domain.vo.MenuVo;
import com.github.cyf.service.PermissionService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 权限信息表 前端控制器
 * </p>
 *
 * @author chenyifan
 * @since 2024-10-16
 */
@RestController
@RequestMapping("/permission")
@Api(tags = "权限操作")
@ApiSupport(order = 2)
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @GetMapping("/selectTree")
    @ApiOperation(value = "1、权限列表树")
    @ApiOperationSupport(order = 1)
    public Rs selectTree() {
        List<MenuVo> menuVos = permissionService.selectTree();
        return Rs.ok().setData(menuVos);
    }

    @PostMapping("/addOrUpdatePermission")
    @ApiOperation("2、添加或修改权限信息")
    @ApiOperationSupport(order = 2)
    @SaCheckPermission(value = {"admin:permission:insert", "admin:permission:update"})
    public Rs addOrUpdatePermission(@RequestBody @Validated Permission permission){
        return Rs.ok().setData(permissionService.saveOrUpdate(permission));
    }

    @GetMapping("/{id}")
    @ApiOperation("3、查询权限")
    @ApiOperationSupport(order = 3)
    @SaCheckPermission(value = "admin:permission:select")
    public Rs getPermission(@PathVariable Long id){
        return Rs.ok().setData(permissionService.getById(id));
    }

    @PostMapping("/delete/{id}")
    @ApiOperation("4、删除权限")
    @ApiOperationSupport(order = 4)
    @SaCheckPermission(value = "admin:permission:delete")
    public Rs deletePermission(@PathVariable Long id){
        return Rs.ok().setData(permissionService.removeById(id));
    }

}
